package com.aptech.restrant.Controller;
import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.aptech.restrant.Bean.AdminDataBean;
import com.aptech.restrant.Model.AdminBean;



public class AdminLogin extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		this.doPost(request, response);
		
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		HttpSession session = request.getSession();
		response.setContentType("text/html;charset=gbk");

	   String content=" ";
	
		String userName = request.getParameter("username");
		String userPass = request.getParameter("password");
		AdminDataBean Adb = new AdminDataBean();
		AdminBean  Ab = Adb.findAdminByName(userName);
		if(Adb.isAdmin(userName, userPass)){
			session.setAttribute("Ab", Ab);
			Cookie ckName = new Cookie("username",Ab.getAdminName());
			ckName.setMaxAge(60*60*24*1);
	  		Cookie ckPass = new Cookie("password",Ab.getAdminPass());
	  		ckPass.setMaxAge(60*60*24*1);
	  		response.addCookie(ckName);
	  		response.addCookie(ckPass);
	  		
	  		//System.out.println(request.getParameter("logins"));
	  		content = "用户登录成功";
	  		session.setAttribute("name",Ab);
		}
		else{
			content = "用户密码错误";		
		}
		session.setAttribute("active", new String(content));
		RequestDispatcher dispatcher = request.getRequestDispatcher("../admin/Active1.jsp");
		dispatcher.forward(request, response);
	}

}
